package com.checkoutcheckins.client.rpc;

import com.checkoutcheckins.client.callbacks.DatabaseCheckinsCallback;
import com.checkoutcheckins.client.data.DataContainer;
import com.checkoutcheckins.client.data.foursquare.FoursquareCheckins;
import com.checkoutcheckins.client.data.foursquare.FoursquareUser;
import com.checkoutcheckins.client.data.foursquare.Settings;
import com.checkoutcheckins.client.services.FoursquareService;
import com.checkoutcheckins.client.services.FoursquareServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class FoursquareRetrival {

	private static FoursquareServiceAsync foursquareService = GWT
			.create(FoursquareService.class);

	public static void retriveFoursquareUser(final String foursquareToken,
			final String foursquareSecret, final DataContainer dataContainer) {

		FoursquareServiceAsync fsSvc = getFoursquareService();

		AsyncCallback<FoursquareUser> callback = new AsyncCallback<FoursquareUser>() {
			public void onFailure(Throwable caught) {
				dataContainer.getStatusPanel().showErrorMessage(
						caught.getMessage(), false);
			}

			@Override
			public void onSuccess(FoursquareUser result) {
				dataContainer.setAndUpdateFoursquareUser(result);
				dataContainer.getStatusPanel().showLoadMessage(
						"Retrieved latest user info", true);

				retrieveUserSettings(result.getId(), dataContainer);
				retriveFoursquareHistory(result.getId(), dataContainer);
			}
		};

		dataContainer.getStatusPanel().showLoadMessage(
				"Getting user from foursquare", false);
		fsSvc.getFoursquareUser(foursquareToken, foursquareSecret, callback);

	}

	public static void retriveFoursquareHistory(String foursquareid,
			final DataContainer dataContainer) {

		FoursquareServiceAsync fsSvc = getFoursquareService();

		AsyncCallback<CheckinResponse> callback = new DatabaseCheckinsCallback(
				fsSvc, dataContainer, true);

		dataContainer.getStatusPanel().showLoadMessage(
				"Getting stored checkins", false);

		fsSvc.getCheckinsFromDatabase(foursquareid, 0, callback);

	}

	public static void retrieveUserSettings(String foursquareId,
			final DataContainer dataContainer) {
		FoursquareServiceAsync fsSvc = getFoursquareService();

		AsyncCallback<Settings> callback = new AsyncCallback<Settings>() {
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(Settings result) {
				dataContainer.setUserSettings(result);
			}
		};

		fsSvc.getUserSettings(foursquareId, callback);
	}

	public static void retriveFoursquareVenue(String foursquareToken,
			String foursquareSecret) {

		FoursquareServiceAsync fsSvc = getFoursquareService();

		AsyncCallback<FoursquareCheckins> callback = new AsyncCallback<FoursquareCheckins>() {
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(FoursquareCheckins result) {
				// dataContainer.setAndUpdateFoursquareCheckins(result);
			}
		};

		// fsSvc.getFoursquareHistory(foursquareToken, foursquareSecret,
		// callback);

	}

	public static FoursquareServiceAsync getFoursquareService() {

		if (foursquareService == null) {
			foursquareService = GWT.create(FoursquareService.class);
		}

		return foursquareService;
	}
}
